package com.hzhr.iot.cqgas.ds.controller;

import com.hzhr.iot.core.constant.CommonOptions;
import com.hzhr.iot.core.util.HzhrDateUtil;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.cqgas.ds.dao.device.custom.DevCollectDataCustomDao;
import com.hzhr.iot.cqgas.ds.dao.device.custom.DeviceDao;
import com.hzhr.iot.cqgas.ds.dao.device.example.*;
import com.hzhr.iot.cqgas.ds.dao.smartmeter.custom.SmCollectDataCustomDao;
import com.hzhr.iot.cqgas.ds.dao.smartmeter.custom.SmDao;
import com.hzhr.iot.cqgas.ds.dao.smartmeter.custom.SmDeviceCustomDao;
import com.hzhr.iot.cqgas.ds.dao.smartmeter.custom.SmGasUserCustomDao;
import com.hzhr.iot.cqgas.ds.dao.smartmeter.example.SmMeterDao;
import com.hzhr.iot.cqgas.ds.dao.smartmeter.example.SmNodeNetworkDao;
import com.hzhr.iot.cqgas.ds.dao.toll.custom.TollGasUserCustomDao;
import com.hzhr.iot.cqgas.ds.dao.toll.example.TollGasUserDao;
import com.hzhr.iot.cqgas.ds.dao.user.example.UsrCompanyDao;
import com.hzhr.iot.cqgas.ds.dao.user.example.UsrDeptDao;
import com.hzhr.iot.cqgas.ds.dao.user.example.UsrUserDao;
import com.hzhr.iot.cqgas.ds.entity.custom.CustomEntity1;
import com.hzhr.iot.cqgas.ds.entity.device.*;
import com.hzhr.iot.cqgas.ds.entity.toll.TollGasUserEntity;
import com.hzhr.iot.cqgas.ds.entity.toll.TollGasUserExample;
import com.hzhr.iot.cqgas.ds.entity.user.UsrCompanyEntity;
import com.hzhr.iot.cqgas.ds.entity.user.UsrDeptEntity;
import com.hzhr.iot.cqgas.ds.entity.user.UsrDeptExample;
import com.hzhr.iot.cqgas.ds.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.Future;

@RestController
public class SmController {
    @Autowired
    CollectingDataService collectingDataService;
    @Autowired
    DevIotDeviceDao devIotDeviceDao;
    @Autowired
    DevCollectDeviceDao devCollectDeviceDao;
    @Autowired
    DevOptionsService devOptionsService;
    @Autowired
    StatisService statisService;
    @Autowired
    TollGasUserDao tollGasUserDao;
    @Autowired
    UsrDeptDao usrDeptDao;
    @Autowired
    CompanyService companyService;
    @Autowired
    UsrCompanyDao usrCompanyDao;
    @Autowired
    ProdFactoryService prodFactoryService;
    @Autowired
    DevProdFactoryDao devProdFactoryDao;
    @Autowired
    DeptService deptService;
    @Autowired
    CollectDeviceTypeService collectDeviceTypeService;
    @Autowired
    DevCollectDeviceTypeDao devCollectDeviceTypeDao;
    @Autowired
    IotDeviceTypeService iotDeviceTypeService;
    @Autowired
    DevIotDeviceTypeDao devIotDeviceTypeDao;
    @Autowired
    GasUserService gasUserService;
    @Autowired
    UserService userService;
    @Autowired
    UsrUserDao usrUserDao;
    @Autowired
    IotDeviceService iotDeviceService;
    @Autowired
    CollectDeviceService collectDeviceService;
    @Autowired
    SmDeviceCustomDao smDeviceCustomDao;
    @Autowired
    SmMeterDao smMeterDao;
    @Autowired
    SmNodeNetworkDao smNodeNetworkDao;
    @Autowired
    SmCollectDataCustomDao smCollectDataCustomDao;
    @Autowired
    DevCollectDataDao devCollectDataDao;
    @Autowired
    DevCollectDataCustomDao devCollectDataCustomDao;
    @Autowired
    TollGasUserCustomDao tollGasUserCustomDao;
    @Autowired
    SmGasUserCustomDao smGasUserCustomDao;
    @Autowired
    TollOptionsService tollOptionsService;
    @Value("${companyId}")
    String companyId;
    @Autowired
    DeviceDao deviceDao;
    @Autowired
    SmDao smDao;

    private Logger logger = LoggerFactory.getLogger(SmController.class);


    @RequestMapping(value = "/sm/syncCollectingData")
    public void syncCollectingData() throws Exception {
        String date = HzhrDateUtil.dateToStr(HzhrDateUtil.addDay(new Date(), -1), "yyyy-MM-dd") + " 00:00:00";
        List<Map<String, Object>> collectDatas = deviceDao.select6(date);
        for (Map<String, Object> collectData :
        collectDatas) {
            String code = HzhrTypeUtil.object2string(collectData.get("CODE"));
            CustomEntity1 device = smDao.select2(code);
            device.setCdTime(collectData.get("CURRENT_TM"));
            device.setCurrentValue(HzhrTypeUtil.object2string(collectData.get("CURRENT_VALUE")));
            Random random = new Random();
            Long id = random.nextLong();
            if (id < 0) {
                id = id * -1;
            }
            String ids = id.toString();
            if (ids.length() > 13) {
                ids = ids.substring(0, 13);
            }
            device.setCdId("N" + ids);
            smDao.insert1(device);
        }
    }
}
